package com.GLM.Viewer.Map;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.util.Log;

public class MyLocationManager {
	private final String TAG = "MyLocationManager";

	private static final int MINTIME = 2000;
	private static final int MININSTANCE = 2;
	private static Context mContext;
	private static LocationListener mListener;
	private static MyLocationManager instance;

	private LocationManager gpsLocationManager;
	private LocationManager networkLocationManager;

	private Location lastLocation = null;

	public static void init() {
	}

	private MyLocationManager() {
		// Gps 定位
		gpsLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
		Location gpsLocation = gpsLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINTIME, MININSTANCE, mListener);
		// 基站定位
		networkLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
		Location networkLocation = gpsLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		networkLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINTIME, MININSTANCE, mListener);
	}

	public static MyLocationManager getInstance(Context c, LocationListener listener) {
		if (null == instance) {
			mContext = c;
			mListener = listener;
			instance = new MyLocationManager();
		}
		return instance;
	}

	public Location getMyLocation() {
		return lastLocation;
	}

	public void destoryLocationManager() {
		Log.d(TAG, "destoryLocationManager");
		gpsLocationManager.removeUpdates(mListener);
		networkLocationManager.removeUpdates(mListener);
	}
}
